package emqx;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class AuthTest {

    public static void main(String[] args) {
        /*String appId = "8fa1adb8dd9ee";
        String appsecret = "MjkyOTU4MTY5ODE1NTA0MDIyNDM4MjQzNjQyODY1MDkwNTG";
        System.out.println("auth:"+getAuthToken(appId,appsecret));*/

        //auth:NTFlNWYwMDNlZWMxYzpNamt3TmpNek1EQXhPRFkyTkRFeE9EUTRNamMwTWpNNE5qUXdNRGMwTWpZd05ESQ==
        //auth:OGZhMWFkYjhkZDllZTpNamt5T1RVNE1UWTVPREUxTlRBME1ESXlORE00TWpRek5qUXlPRFkxTURrd05URw==
        //Basic OGZhMWFkYjhkZDllZTpNamt5T1RVNE1UWTVPREUxTlRBME1ESXlORE00TWpRek5qUXlPRFkxTURrd05URw==

        /*String username = "caoxiabo";
        String password = "caoxiabo-secret";
        System.out.println("Basic = [" + getAuthToken(username,password) + "]");*/

        String appId = "efd7343ee8725";
        String appsecret = "Mjk0MDU2MDkzMDU2NjAxNjczOTEyODUzNDI4NTE2OTQ1OTC";
        System.out.println("auth:"+getAuthToken(appId,appsecret));

        //ZWZkNzM0M2VlODcyNTpNamswTURVMk1Ea3pNRFUyTmpBeE5qY3pPVEV5T0RVek5ESTROVEUyT1RRMU9UQw==
        //Basic ZWZkNzM0M2VlODcyNTpNamswTURVMk1Ea3pNRFUyTmpBeE5qY3pPVEV5T0RVek5ESTROVEUyT1RRMU9UQw==
    }

    /**
     *@Author: caoxiabo on 2019/12/16 14:19
     *@Description: 通过appId,appsecret获取auth,准确是获取106.52.3.122节点的auth
     * curl -v --basic -u <appid>:<appsecret> -k http://106.52.3.122:8080/api/v3/brokers
     */
    private static String getAuthToken(String appId,String appsecret){
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = appId+":"+appsecret;
        byte[] textByte;
        try {
            textByte = text.getBytes("UTF-8");
            //编码
            final String encodedText = encoder.encodeToString(textByte);
            return encodedText;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
